package org.example.cprobes;

import org.example.cprobes.gui.MonitorGui;

import fr.imag.adele.cprobes.abstracts.AbstractProbe;
import fr.imag.adele.cprobes.services.DataCollector;
import fr.imag.adele.cprobes.services.DataSender;
import fr.imag.adele.cprobes.services.Scheduler;
import fr.imag.adele.cprobes.util.DataSet;

public class Monitor extends AbstractProbe {	
	private MonitorGui m_monitor;	
	
	public void process() {
		DataSet dataSet = getCollectedData();
		if (dataSet != null){
			if (m_monitor != null){
				for (int i=0; i<dataSet.size(); i++){
					m_monitor.addOutput(dataSet.getDataEvent(i).getResource() + ": " + 
							dataSet.getDataEvent(i).getValue() + " (from: " + dataSet.getDataEvent(i).getSource() + ") [timestamp: " +
							dataSet.getDataEvent(i).getTimeStamp()+ "]"										
					);
					m_monitor.addCapture(dataSet.getDataEvent(i).getSource() + "/" + dataSet.getDataEvent(i).getResource() ,new Double(dataSet.getDataEvent(i).getValue().toString()).doubleValue());
					System.out.print(getProbeId() + ": ");
					System.out.println(dataSet.getDataEvent(i).getResource() + ": " + 
							dataSet.getDataEvent(i).getValue() + " (from: " + dataSet.getDataEvent(i).getSource() + ") [timestamp: " +
							dataSet.getDataEvent(i).getTimeStamp()+ "]");
					if (i == dataSet.size() - 1) m_monitor.addOutput(".");
				}
				
			}
		}
	}
		
	public void starting() {		
		super.starting();
		String[] s = getDataCollector().getRequiredResources();
		m_monitor = new MonitorGui("Monitor: " + getProbeId(), s, this);
		m_monitor.setVisible(true);
	}	
	public void stoping() {	
		super.stoping();
		//m_monitor.dispose();
	}
	////Binding methods
	// TODO if we can bind directly with methods from the parent class
	// then we have to move these methods to the parent class.
	
	public void bindScheduler(Scheduler scheduler) {			
		setScheduler(scheduler);
	}
	public void unbindScheduler() {
		setScheduler(null);
	}
	public void bindDataCollector(DataCollector dataCollector) {
		setDataCollector(dataCollector);
	}
	public void unbindDataCollector() {
		setDataCollector(null);
	}
	public void bindDataSender(DataSender dataSender) {
		setDataSender(dataSender);
	}
	public void unbindDataSender() {
		setDataSender(null);
	}
	
	////
}
